const { app } = require('./express');
const { querySql } = require('./mysql')

app.get('/test', (req, res) => {
    res.send("hello")
})

const resultData = {
    code: '',
    msg: '',
    data: ''
}
app.post('/test/goods', (req, res) => {
    let sql = `select * FROM goods`;
    let sqlParam = [];
    querySql(sql, sqlParam, (err, result) => {
        const baseReturn = returnData('2000', '查询成功', result);
        res.send(baseReturn)
    })
})
app.post('/foo/test/goods', (req, res) => {
    let sql = `select * FROM goods`;
    let sqlParam = [];
    querySql(sql, sqlParam, (err, result) => {
        const baseReturn = returnData('2000', '查询成功', result);
        res.send(baseReturn)
    })
})

app.post('/goods/query', (req, res) => {
    let size = req.body.size - 0;
    let page = (req.body.page - 1) * size;
    let sql = `select * FROM goods LIMIT ?,?`;
    let sqlParam = [page, size];
    querySql(sql, sqlParam, (err, result) => {
        if (err) {
            const baseReturn = returnData('5000', '查询失败');
            res.send(baseReturn)
        } else {
            let sql = 'SELECT count(id) as total from goods'
            querySql(sql, [], (err, result1) => {
                const baseReturn = returnData('2000', '查询成功', result);
                console.log(result1);
                //转化成json字符串
                const objStr = JSON.stringify(result1);
                //再转化成对象
                const obj = JSON.parse(objStr);
                baseReturn.total = obj[0].total;
                res.send(baseReturn)
            })

        }
    })
});


app.post('/goods/insert', (req, res) => {
    let sql = '';
    let sqlParam = '';
    console.log(req.body);
    if (req.body.status == 'active') {
        var date = new Date();
        console.log(date);
        sqlParam = [req.body.name, req.body.market_price, req.body.price, req.body.purchase_price, req.body.num, req.body.status, req.body.make, date];
        sql = 'insert into goods(name,market_price,price,purchase_price,num,status,make,pushDate) values(?,?,?,?,?,?,?,?)';
    } else {
        console.log(req.body.market_price);
        sqlParam = [req.body.name, req.body.market_price, req.body.price, req.body.purchase_price, req.body.num, req.body.status, req.body.make];
        sql = 'insert into goods(name,market_price,price,purchase_price,num,status,make) values(?,?,?,?,?,?,?)';
    }
    querySql(sql, sqlParam, (err, result) => {
        if (err) {
            const baseReturn = returnData('5000', '添加失败');
            res.send(baseReturn)
        } else {
            const baseReturn = returnData('2000', '添加成功', result);
            res.send(baseReturn)
        }
    })
});

//修改商品上架或者下架的接口
//1.如果传来一个true 那就是上架
//2.否则传来一个false 那就是下架
app.post('/goods/updateStatusById', (req, res) => {
    //id 状态拿到
    let status = '';
    let msg = '';
    let sql = 'update  goods set status=? where id = ?';
    if (req.body.delivery) {
        status = 'active'
        msg = '上架';
    } else {
        status = 'unactive'
        msg = '下架';
    }
    let sqlParam = [status, req.body.id]
    querySql(sql, sqlParam, (err, result) => {
        if (err) {
            const baseReturn = returnData('5000', '修改失败');
            res.send(baseReturn)
        } else {
            const baseReturn = returnData('2000', msg + '成功');
            res.send(baseReturn)
        }
    })

})

app.post('/goods/getById', (req, res) => {
    let sql = 'select * from goods where id = ?';
    let sqlParam = [req.body.id]
    querySql(sql, sqlParam, (err, result) => {
        if (err) {
            const baseReturn = returnData('5000', '查询失败');
            res.send(baseReturn)
        } else {
            const baseReturn = returnData('2000', '查询成功', result);
            res.send(baseReturn)
        }
    })
})
app.post('/goods/updateById', (req, res) => {
    let sql = 'update  goods set name=?,market_price=?,price=?,purchase_price=?,num=?,status=? where id = ?';
    let sqlParam = [req.body.name, req.body.market_price, req.body.price, req.body.purchase_price, req.body.num, req.body.status, req.body.id]
    querySql(sql, sqlParam, (err, result) => {
        if (err) {
            const baseReturn = returnData('5000', '修改失败');
            res.send(baseReturn)
        } else {
            const baseReturn = returnData('2000', '修改成功');
            res.send(baseReturn)
        }
    })
})
app.post('/goods/delById', (req, res) => {
    let sql = 'delete from goods where id = ?';
    let sqlParam = [req.body.id]
    querySql(sql, sqlParam, (err, result) => {
        if (err) {
            const baseReturn = returnData('5000', '删除失败');
            res.send(baseReturn)
        } else {
            const baseReturn = returnData('2000', '删除成功');
            res.send(baseReturn)
        }
    })
})

app.post('/goods/pull', (req, res) => {
    //当前id
    let id = req.body.id;
    //原有的数量 
    let num = req.body.num - 0;
    //入库的数量
    let pullNum = req.body.pull_num - 0;
    //现在的商品数量 = 入库的数量+原有的数量;
    num = num + pullNum;
    let sql = 'update  goods set num=? where id = ?';
    let sqlParam = [num, id]
    querySql(sql, sqlParam, (err, result) => {
        if (err) {
            const baseReturn = returnData('5000', '入库失败');
            res.send(baseReturn)
        } else {
            const baseReturn = returnData('2000', '入库成功');
            createCustomerBill(id, pullNum);
            res.send(baseReturn)
        }
    })

})
const createCustomerBill = (id, pullNum) => {
    //商品的名称
    //入库的id id
    //入库数量 pullNum
    //进价 purchase_price
    //支付方式pay_type  wx_pay
    //订单状态 status  payment
    let sql = 'select * from goods where id = ?';
    let sqlParam = [id]
    querySql(sql, sqlParam, (err, result) => {
        if (err) {
            console.log(err);
        } else {
            //转化成json字符串
            const goodsStr = JSON.stringify(result);
            //再转化成对象
            const goods = JSON.parse(goodsStr);
            sql = 'insert into customer_bill(goods_id,name,num,purchase_price,pay_type,status) values(?,?,?,?,?,?)';
            sqlParam = [id, goods[0].name, pullNum, goods[0].purchase_price, 'wx_pay', 'payment'];
            querySql(sql, sqlParam, (err, res) => {
                if (err) {
                    console.log("-------创建商户订单失败了---------");
                } else {
                    console.log("-------创建商户订单成功了---------");
                }
            });
        }
    })
}

function returnData(code, msg, data) {
    resultData.code = code;
    resultData.msg = msg;
    resultData.data = data;
    return resultData;
}
app.post('/query/customer_bill', (req, res) => {
    let size = req.body.size - 0;
    let page = (req.body.page - 1) * size;
    let name = req.body.name;
    console.log(name);
    let sql = '';
    let sqlParam = [];
    if (name === undefined || name === '') {
        sql = 'select * from customer_bill limit ?,?';
        sqlParam = [page, size];
    } else {
        sql = 'select * from customer_bill where name = ? limit ?,?';
        sqlParam = [name, page, size];
    }
    querySql(sql, sqlParam, (err, result) => {
        if (err) {
            const baseReturn = returnData('5000', '查询失败');
            res.send(baseReturn)
        } else {
            let sql = 'SELECT count(id) as total from customer_bill'
            querySql(sql, [], (err, result1) => {
                const baseReturn = returnData('2000', '查询成功', result);
                console.log(result1);
                //转化成json字符串
                const objStr = JSON.stringify(result1);
                //再转化成对象
                const obj = JSON.parse(objStr);
                baseReturn.total = obj[0].total;
                res.send(baseReturn)
            })
        }
    })
})